package com.xuzhijie.pms.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.Owner;
import com.xuzhijie.pms.entity.pojo.UserRequest;
import com.xuzhijie.pms.service.IOwnerService;
import com.xuzhijie.pms.utils.WechatDecryptUtil;
import com.xuzhijie.pms.utils.saltUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-16
 */
@RestController
@RequestMapping("/owner")
public class OwnerController {

    @Resource
    private IOwnerService iOwnerService;

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer page,
                           @RequestParam Integer size,
                           @RequestParam Integer isRefine,
                           @RequestParam(required = false) Date startTime,
                           @RequestParam(required = false) Date endTime,
                           @RequestParam String phone)
    {

        return iOwnerService.findPage(page,size,isRefine,startTime,endTime,phone);
    }

    @GetMapping("/ownerInfo")
    public Result getOwnerInfo(@RequestParam String ownerId){
        return iOwnerService.getOwnerInfo(ownerId);
    }

    @PutMapping("/unbinding")
    public Result unbindingProperty(@RequestBody String id){
        return iOwnerService.unbindingProperty(id);
    }

    @GetMapping("/getowner")
    public Result getOwnerByPhone(@RequestParam String phone){
        return iOwnerService.getOwnerByPhone(phone);
    }

    @PostMapping("/getUser")
    public String getUser(@RequestBody UserRequest userRequest) throws Exception {
        // 在这里处理前端发送的请求，获取用户手机号
        String encryptedData = userRequest.getEncryptedData();
        String iv = userRequest.getIv();
        String sessionkey = userRequest.getCode();
        String s = WechatDecryptUtil.decryptData(encryptedData, sessionkey, iv);


        System.out.println("encryptedData："+encryptedData);
        System.out.println("iv："+iv);
        System.out.println(s);
        return "Success";
    }

    @PostMapping("/getSessionKey")
    public String getSessionKey(@RequestParam("code") String code) {
        // 向微信服务器发送请求，获取 sessionKey
        String appId = "wx3a4160cb7f46a56f"; // 小程序的 AppID
        String appSecret = "6f697938abd30e8e2e3ac89514fa81a8"; // 小程序的 AppSecret
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=authorization_code";

        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);
        return response;
    }



    @GetMapping("/login")
    public Result ownerLogin(@RequestParam String phone,
                             @RequestParam String password,
                             @RequestParam String nickName,
                             @RequestParam String face){
        System.out.println(face);
        return iOwnerService.ownerLogin(phone,password,nickName,face);
    }

    @GetMapping("/register")
    public Result ownerregister(@RequestParam String phone,
                             @RequestParam String password,
                             @RequestParam String nickName,
                             @RequestParam String face){
        System.out.println("访问注册");
        return iOwnerService.ownerregister(phone,password,nickName,face);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Map<String,Object> req){
        Object idcard = req.get("idcard");
        Object nickname = req.get("nickname");
        Object gender = req.get("gender");
        Object sign = req.get("sign");
        String id = req.get("ownerId").toString();
        String password = req.get("password").toString();
        Owner byId = iOwnerService.getById(id);

        if (idcard != null){
            byId.setIdCard(idcard.toString());
            byId.setIsRefine(1);
        }

        if (nickname != null){
            byId.setNickName(nickname.toString());
        }
        if (gender != null){
            byId.setGender(Integer.parseInt(gender.toString()));
        }

        if (sign != null){
            byId.setSign(sign.toString());
        }

        if(StrUtil.isNotBlank(password)){
            String salt = byId.getSalt();
            String password1 = saltUtil.encodePassword(password, salt);
            byId.setPassword(password1);
        }

        iOwnerService.saveOrUpdate(byId);

        return Result.success();
    }

}

